<?php

namespace App\Http\Controllers\WebPage;

/**
 * 返回文章列表与文章详情api
 */

use App\Http\Controllers\Controller;
use App\Http\Models\Article;
use App\Http\Models\ArticleTag;

use App\Http\Requests\WebPage\Article\ArticleIndexRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class ArticleController extends Controller
{
    use ApiResponser, ApiChecker;

    const DEFAULT_PER_PAGE = 6;

    const LATEST_COLUMN = 'created_at';

    /**
     * 返回新闻文章列表
     * TODO tag 标签搜索
     *
     * @param ArticleIndexRequest $request
     * @return array
     */
    public function index(ArticleIndexRequest $request)
    {

        $articles_paginate = Article::select($this->articleIndexSelectColumns())
            ->ArticleTagId($request->tag_id)
            ->latest(self::LATEST_COLUMN)
            ->paginate($request->per_page ?: self::DEFAULT_PER_PAGE);

        return $this->response(compact('articles_paginate'));
    }


    /**
     * 保留,首页
     * @param ArticleIndexRequest $request
     * @return array
     */
    public function tagArticle(ArticleIndexRequest $request)
    {

        $article_tag = ArticleTag::find($request->tag_id);  //主类(标签)的ID
        $this->checkItemExist($article_tag,'标签');

        $tags = $article_tag->tags;

        $tags->transform(function ( $tag)  {

            $article_ids = DB::table('article_pivot_tag')->where('article_tag_id',$tag->id)->pluck('article_id');

            $tag->articles = Article::whereIn('id',$article_ids)
                ->select($this->articleIndexSelectColumns())
                ->latest(self::LATEST_COLUMN)
                ->take(10)
                ->get();

            return $tag;
        });

        return $this->response(compact('tags'));
    }


    /**
     * @return array
     */
    private function articleIndexSelectColumns()
    {
        return [
            'id' , 'thumb' , 'banner' , 'description', 'created_at', 'title','carousel_figure','company_thumb'
        ];
    }

    /**
     * 返回指定文章
     *
     * @param $article_id
     * @return array
     */
    public function show($article_id)
    {
        $article = Article::find($article_id);
            $this->checkItemExist($article);

        return $this->response(compact('article'));
    }

    /**
     * 返回所有标签
     * @return array
     */
    public function create()
    {

        $tags = ArticleTag::select('id','super_tag_id','name')->where('super_tag_id',null)->get();

        $tags->transform(function ( $tag)  {
            $tag->sub_tag = $tag->tags()->orderBy('sort','ASC')->select('id','super_tag_id','name')->get();

            return $tag;
        });

        return $this->response(compact('tags'));
    }




    //旧版新闻展示
    public function articleList()
    {

        $articles = Article::orderBy('id','DESC')->paginate(5);

        return view('bym_news',compact('articles'));
    }

    public function articleDetail($id)
    {
        $article = Article::find($id);
        $previous = Article::where('id','<',$id)->orderBy('id','DESC')->first();
        $next = Article::where('id','>',$id)->orderBy('id','ASC')->first();
        return view('bym_article',compact(
            'article',
            'previous',
            'next'
        ));
    }

}
